﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QFlow.Data;

namespace QFlow.Windows.Forms
{
    public partial class RolesList : Utils.BaseForm
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RolesList"/> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public RolesList(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Load event of the RolesList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RolesList_Load(object sender, EventArgs e)
        {
            this.RolesBindingSource.DataSource = this.Context.Roles;
        }

        /// <summary>
        /// Handles the OptionsClick event of the warning control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void warning_OptionsClick(object sender, EventArgs e)
        {
            try
            {
                this.Context.SubmitChanges();
                this.warning.Hide();
            }
            catch
            {
                MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantDeleteRole, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Handles the UserDeletingRow event of the gvRoles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewRowCancelEventArgs"/> instance containing the event data.</param>
        private void gvRoles_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
        {
            this.warning.Show();
        }

        /// <summary>
        /// Handles the CellDoubleClick event of the gvRoles control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.DataGridViewCellEventArgs"/> instance containing the event data.</param>
        private void gvRoles_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvRoles.SelectedRows.Count > 0)
            {
                int roleId = (gvRoles.SelectedRows[0].DataBoundItem as Role).RoleID;
                NewRole frm = new NewRole(roleId, this.Context);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        /// <summary>
        /// Handles the Activated event of the RolesList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RolesList_Activated(object sender, EventArgs e)
        {
            this.RolesBindingSource.DataSource = this.Context.Roles;
        }

        /// <summary>
        /// Handles the FormClosing event of the RolesList control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.FormClosingEventArgs"/> instance containing the event data.</param>
        private void RolesList_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Context.DiscardChanges();
        }
    }
}
